package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.cache.redis.service.IStrategyStatesVoService;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.uitl.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("users")
public class UserInfoController {
    //拿配置文件数据
    @Value("${sms.url}")
    private String url;
    @Value("${sms.appkey}")
    private String appkey;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IStrategyStatesVoService strategyStatesVoService;
    //通过手机码查询客户
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean le =userInfoService.findByPhone(phone);
        return le;
    }
    //发送短信,拿到验证码
    @GetMapping("/sendVerifyCode")
    public JsonResult sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone,url,appkey);
        return JsonResult.success();
    }
    //
    //完成注册,返回是否成功的数据
    @PostMapping("/regist")
    public JsonResult regist(String phone,String nickname ,String password ,String rpassword, String verifyCode){
        //发送到后台处理业务,异常就统一处理,只返回成功的就可以啦
        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
        return JsonResult.success();
    }
    //登陆功能,发的是post请求,返回值是整个JsonResult
    @PostMapping("/login")
    public JsonResult login(String username,String password ){

        //通过唯一的号码和密码查确定对象
        UserInfo user = userInfoService.logins(username,password);
        //来到这里,证明账户没有问题,把账户放到redis中
        String token=userInfoService.setToken(user);
        //把对象和令牌传到前端
        HashMap map = new HashMap();
        map.put("user",user);
        map.put("token",token);
        //直接把data值调用方法放进去
        return JsonResult.success(map);
    }

    /**
     * 摸以第二次请求共享当前用户
     * @return
     */
    @RequireLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        //利用请求头拿到令牌
        String token = request.getHeader("token");
        //摸以
        //token ="user_loginkey_token:a5fa3b255a8b423495a4a4916139d9cf";
       /* if (!StringUtils.hasLength(token)){
            throw  new LogiclException("请登录");
        }*/
        //利用令牌拿到redis中对应的对象
        UserInfo user =userInfoService.getKeyUser(token);
       /* //判断
        if (user ==null){
            throw  new LogiclException("请登录");
        }*/
        return JsonResult.success(user);
    }

    @GetMapping("/strategies/favor")
    public Object favor(String sid,String userId){
        //返回的是一个redis中key对应的集合
        return JsonResult.success(strategyStatesVoService.gekyeList(sid,userId));
    }

}
